home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / misc / LEDA_high.lha / LEDA-3.1c-high / man / Misc.tex < prev    next >
Encoding:
Text File  |  1994-08-05  |  7.5 KB  |  260 lines

  1. {\magtwo 7. Miscellaneous}
  2. \bigskip
  3. \bigskip
  4.  
  5. This section describes some additional useful data types, functions and
  6. macros of LEDA. They can be used in any program that includes the
  7. $<$LEDA/basic.h$>$ header file.
  8.  
  9. \bigskip
  10. \bigskip
  11. {\magonebf 7.1 Streams}
  12.  
  13. The stream data types described in this section are all derived from
  14. the \CC stream types $istream$ and $ostream$. Some of these types 
  15. may be obsolete in combination with the latest versions of the 
  16. standard \CC I/O library.
  17.  
  18. \bigskip
  19. \bigskip
  20. {\magonebf 7.1.1 File input streams (file\_istream)}
  21.  
  22. {\bf 1. Definition}
  23.  
  24. An instance $I$ of the data type $file\_istream$ is an \CC istream
  25. connected to a file $F$, i.e., all input operations or operators
  26. applied to $I$ read from $F$. 
  27.  
  28. {\bf 2. Creation}
  29.  
  30. $file\_istream\ \ \ I(string\ s)$;
  31.  
  32. creates an instance $I$ of type file\_istream connected to the file with
  33. name $s$.
  34.  
  35. {\bf 3. Operations} 
  36.  
  37. All operations and operators ($>>$) defined for \CC istreams can
  38. be applied to file input streams as well.
  39.  
  40. \bigskip
  41. \bigskip
  42. {\magonebf 7.1.2 File output streams (file\_ostream)}
  43.  
  44. {\bf 1. Definition}
  45.  
  46. An instance $O$ of the data type $file\_ostream$ is an \CC ostream
  47. connected to a file $F$, i.e., all output operations or operators
  48. applied to $O$ write to $F$. 
  49.  
  50. {\bf 2. Creation}
  51.  
  52. $file\_ostream\ \ \ O(string\ s)$;
  53.  
  54. creates an instance $O$ of type file\_ostream connected to the file with
  55. name $s$.
  56.  
  57. {\bf 3. Operations} 
  58.  
  59. All operations and operators ($<<$) defined for \CC ostreams can
  60. be applied to file output streams as well.
  61.  
  62. \bigskip
  63. {\magonebf 7.1.3 String input streams (string\_istream)}
  64.  
  65. {\bf 1. Definition}
  66.  
  67. An instance $I$ of the data type $string\_istream$ is an \CC istream
  68. connected to a string $s$, i.e., all input operations or operators
  69. applied to $I$ read from $s$. 
  70.  
  71. {\bf 2. Creation}
  72.  
  73. $string\_istream\ \ \ I(string\ s)$;
  74.  
  75. creates an instance $I$ of type string\_istream connected to the string $s$.
  76.  
  77. {\bf 3. Operations} 
  78.  
  79. All operations and operators ($>>$) defined for \CC istreams can
  80. be applied to string input streams as well.
  81.  
  82. \bigskip
  83. {\magonebf 7.1.4 String output streams (string\_ostream)}
  84.  
  85. {\bf 1. Definition}
  86.  
  87. An instance $O$ of the data type $string\_ostream$ is an \CC ostream
  88. connected to an internal string buffer, i.e., all output operations 
  89. or operators applied to $O$ write into this internal buffer.  The current
  90. value of the buffer is called the contents of $O$.
  91.  
  92. {\bf 2. Creation}
  93.  
  94. $string\_ostream\ \ \ O$;
  95.  
  96. creates an instance $O$ of type string\_ostream.
  97.  
  98. {\bf 3. Operations} 
  99. \medskip
  100. \cleartabs
  101. \+\hskip 1.8truecm &\hskip 5truecm &\cr
  102. \medskip
  103. \+string      &O.clear()  &clears the contents of $O$\cr
  104. \medskip
  105. \+string      &O.str()    &returns the current contents of $O$\cr
  106. \smallskip
  107.  
  108. All operations and operators ($<<$) defined for \CC ostreams can
  109. be applied to string output streams as well.
  110.  
  111.  
  112. \bigskip
  113. {\magonebf 7.1.5 Command input streams (cmd\_istream)}
  114.  
  115. {\bf 1. Definition}
  116.  
  117. An instance $I$ of the data type $cmd\_istream$ is an \CC istream
  118. connected to the output of a shell command $cmd$, i.e., all input operations 
  119. or operators applied to $I$ read from the standard output of command $cmd$. 
  120.  
  121. {\bf 2. Creation}
  122.  
  123. $cmd\_istream\ \ \ I(string\ cmd)$;
  124.  
  125. creates an instance $I$ of type cmd\_istream connected to the output of command $cmd$.
  126.  
  127. {\bf 3. Operations} 
  128.  
  129. All operations and operators ($>>$) defined for \CC istreams can
  130. be applied to command input streams as well.
  131.  
  132. \bigskip
  133. {\magonebf 7.1.6 Command output streams (cmd\_ostream)}
  134.  
  135. {\bf 1. Definition}
  136.  
  137. An instance $O$ of the data type $cmd\_ostream$ is an \CC ostream
  138. connected to the input of a shell command $cmd$, i.e., all output operations 
  139. or operators applied to $O$ write into the standard input of command $cmd$. 
  140.  
  141. {\bf 2. Creation}
  142.  
  143. $cmd\_ostream\ \ \ O(string\ cmd)$;
  144.  
  145. creates an instance $O$ of type cmd\_ostream connected to the input of 
  146. command $cmd$. 
  147.  
  148. {\bf 3. Operations} 
  149.  
  150. All operations and operators ($<<$) defined for \CC ostreams can
  151. be applied to command output streams as well.
  152.  
  153.  
  154. \bigskip
  155. \bigskip
  156. {\magonebf 7.2 Some useful functions and macros}
  157. \smallskip
  158. \cleartabs
  159. \+\hskip 1.8truecm &\hskip 5truecm &\cr
  160. \medskip
  161. \+int      &read\_int(string $s$ = ``'')    
  162.                              &prints $s$ and reads an integer\cr
  163. \smallskip
  164. \+char     &read\_char(string $s$ = ``'')   
  165.                              &prints $s$ and reads a character\cr
  166. \smallskip
  167. \+double   &read\_real(string $s$ = ``'')   
  168.                              &prints $s$ and reads a real number\cr
  169. \smallskip
  170. \+string   &read\_string(string $s$ = ``'') 
  171.                              &prints $s$ and reads a line of input\cr
  172. \smallskip
  173. \+bool     &Yes(string $s$ = ``'')          
  174.                              &returns (read\_char($s$) == `y')\cr
  175. \bigskip
  176. \+void   &init\_random()     &initializes the random number generator.\cr
  177. \smallskip
  178. \+double &random()           &returns a real valued random number in $[0,1]$\cr
  179. \smallskip
  180. \+int    &random(int a, int b) &returns a random integer in $[a..b]$\cr
  181. \bigskip
  182. \+float &used\_time()       &returns the currently used cpu time in seconds.\cr
  183. \smallskip
  184. \+float &used\_time(float\& $T$)  
  185.                              &returns the cpu time used by the program from\cr
  186. \+      &                    &$T$ up to this moment and assings the current\cr
  187. \+      &                    &time to $T$.\cr
  188. \smallskip
  189. \+void  &print\_statistics() &prints a summary of the currently used memory\cr
  190.  
  191. \bigskip
  192. \cleartabs
  193. \+{\bf newline} \hskip 2truecm   &cout $<<$ ``\n"\cr
  194. \+\cr
  195. \+{\bf forever}                  &for(;;)\cr
  196. \+\cr
  197. \+{\bf loop(a,b,c)}              &for ($a=b;a<=c;a++$)\cr
  198. \+\cr
  199. \+{\bf Max(a,b)}                 &($(a>b)\ ?\ a\ :\ b$)\cr
  200. \+\cr
  201. \+{\bf Min(a,b)}                 &($(a>b)\ ?\ b\ :\ a$)\cr
  202.  
  203.  
  204. \vfill\eject
  205.  
  206. \bigskip
  207. \bigskip
  208. {\magonebf 7.3 Memory Management}
  209.  
  210. LEDA offers an efficient memory management system that is used internally 
  211. for all node, edge and item types. This system can easily be customized for 
  212. user defined classes by the ``LEDA\_MEMORY" macro. You simply have
  213. to add the macro call ``LEDA\_MEMORY($T$)" to the declaration of a class
  214. $T$. This creates new and delete operators for type $T$ allocating and
  215. deallocating memory using LEDA's internal memory manager. We continue
  216. the example from section 1.5:
  217. \medskip
  218. \cleartabs
  219. \+{\bf struct} $pair$ $\{$\cr
  220. \+\quad &$double\ \ x$;\cr
  221. \+      &$double\ \ y$;\cr
  222. \+\cr
  223. \+&$pair()\ \{\ x = y = 0;\ \}$\cr
  224. \+&$pair($const $pair\&\ p)\ \{\ x = p.x;\ y = p.y;\ \}$\cr
  225. \+\cr
  226. \+&friend $ostream$\& \ &operator\<\<($ostream$\&,const $pair$\&) $\{$ \dots $\}$\cr
  227. \+&friend $istream$\& \ &operator\>\>($istream$\&,$pair$\&) $\{$ \dots $\}$\cr
  228. \+&friend $int$         &compare(const $pair\&\ p$, const $pair\&\ q$) $\{$ \dots $\}$\cr
  229. \+\cr
  230. \+&LEDA\_MEMORY($pair$)\cr
  231. \+$\}$;\cr
  232. \smallskip
  233. dictionary\<$pair$,$int$\> D;
  234.  
  235.  
  236. \bigskip
  237. \bigskip
  238. {\magonebf 7.4 Error Handling}
  239.  
  240. LEDA tests the preconditions of many (not all!) operations. Preconditions
  241. are never tested, if the test takes more than constant time. If the test
  242. of a precondition fails an error handling routine is called. It takes 
  243. an integer error number $i$ and a $char*$ error message string $s$ as 
  244. arguments. It writes $s$ to the diagnostic output (cerr) and terminates 
  245. the program abnormally if $i \ne 0$.
  246. Users can provide their own error handling function $handler$ by calling 
  247. \smallskip
  248. set\_error\_handler($handler$).
  249. \smallskip 
  250. After this function call $handler$ is used instead of the default error 
  251. handler. $handler$ must be a function of type  $void\ \ handler(int,char*)$. 
  252. The parameters are replaced by the error number and the error message
  253. respectively.
  254.  
  255. \vfill\eject
  256.  
  257. \vglue 10cm
  258. \vfill\eject
  259.  
  260.